import _ from 'lodash';
// import { formComponentRead } from '@/api/projectDetail/projectDetail';
import { useFilmReadRight } from '@/store';
import type * as Type from './type';
// import type { NidusItem, ShowComponentObj } from '../toolBar/type';

// 递归处理级联菜单
export const getOptionData = (dataList: any[]) => {
  if (!dataList) return [];
  dataList = dataList.map(
    (item: { child: any[]; code: string; text: string }) => {
      let obj = {
        value: '',
        label: '',
        children: null as unknown,
      };
      if (item.child && item.child.length > 0) {
        obj.value = item.code;
        obj.label = item.text;
        obj.children = getOptionData(item.child);
      } else {
        obj.value = item.code;
        obj.label = item.text;
      }
      return obj;
    }
  );
  return dataList;
};

// 处理病灶数据集合中的标注征象组件
export const getComponentData = async (resultItem:any, funcType: number) => {
  // const result = await formComponentRead(item.componentId);
  const { componentCode, id, imageAnnotationId,  componentName, componentType, componentParameter, componentData,required, componentId,  ...newRes } = resultItem;
  const tempParameter = JSON.parse(componentParameter || '{}');
  const tempObj: Type.FormOptionsItem = _.cloneDeep({
    id: 0,
    componentId: 0,
    title: '',
    field: '',
    type: '',
    required: false
  });
  tempObj.id = id;
  tempObj.componentId = componentId;
  tempObj.title = componentName;
  tempObj.field = componentCode;
  tempObj.type = componentType;
  tempObj.required = required;
  tempObj.parameter = _.cloneDeep(tempParameter);
  tempObj.data = _.cloneDeep(getOptionData(JSON.parse(componentData || '[]')));
  const FilmReadRight = useFilmReadRight();
  if (funcType === 1) {
    // 序列征象处理
    if (componentType === 'checkbox') {
      FilmReadRight.seriesSettings && (FilmReadRight.seriesSettings.form[componentCode] = []);
    } else {
      FilmReadRight.seriesSettings && (FilmReadRight.seriesSettings.form[componentCode] = '');
    }
    FilmReadRight.seriesSettings.formOptions.push(tempObj);
  } else {
    if (componentType === 'checkbox') {
      FilmReadRight.currentMiniSeriesSet && (FilmReadRight.currentMiniSeriesSet.form[componentCode] = []);
    } else {
      FilmReadRight.currentMiniSeriesSet && (FilmReadRight.currentMiniSeriesSet.form[componentCode] = '');
    }
    FilmReadRight.currentMiniSeriesSet.formOptions.push(tempObj);
    FilmReadRight.currentMiniSeriesSet.formId = resultItem.formId;
    

    // // 标注征象及病灶
    // if (markItem) {
    //   if (type === 'checkbox') {
    //     markItem.seriesList.form[tempObj.field] = [];
    //   } else {
    //     markItem.seriesList.form[tempObj.field] = '';
    //   }
    //   markItem.seriesList.formOptions.push(tempObj);
    // }
  }
}



// 处理标注工具病灶内数据
// export const getLesionList = (type: string, toolList: string[], lesion: string, color: string, imageCompList: Type.ImageCompItem[] | null) => {
//   const ToolBar = useToolBar();
//   const LesionData = useLesionData();
//   // 调用接口次数过多 待优化点
//   if (toolList.find(it => it === type)) {
//     const tempArr = ToolBar.markToolList.filter(it => it.toolType === type);
//     const tempObj: NidusItem = _.cloneDeep({
//       lesion,
//       color,
//       eyeStatus: true,
//       toolType: type,
//       id: 0,
//       name: Reflect.get(LesionData.toJSON(), lesion),
//       seriesList: {
//         formId: 0,
//         form: <{[x: string]: string | []}>{},
//         formOptions: <Type.FormOptionsItem[]>[]
//       }
//     })
//     imageCompList?.map(it => {
//       getComponentData(it, 2, tempObj);
//     })
//     tempArr[0].lesionList.push(tempObj);
//   }
// }

// 处理征象结果数据
export const getComponentResult = (item: Type.SeriesResultItem, type: boolean = false, obj?: any) => {
  const FilmReadRight = useFilmReadRight();
  if (type) {
    FilmReadRight.seriesSettings.formOptions.map(it => {
      if (it.componentId === item.componentId) {
        it.acrId = item.id;
        it.id = item.id;
        if (item.componentId === 4 || item.componentId === 5) {
          typeof item.result === 'string' && (item.result = item.result.split('@#@'));
        }
        FilmReadRight.seriesSettings.form[it.field] = item.result;
      }
    })
  } else {
    obj && obj.formOptions.map((it:any) => {
      if (it.componentId === item.componentId) {
        it.acrId = item.id;
        it.id = item.id;
        if (item.componentId === 4 || item.componentId === 5) {
          typeof item.result === 'string' && (item.result = item.result.split('@#@'));
        }
        obj.form[it.field] = item.result;
      }
    })
  }
}